package com.tongji.showroom.view.titleWindows
{
	import com.tongji.showroom.events.FilterEvent;
	import com.tongji.showroom.model.FurnitureModel;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.managers.PopUpManager;
	import mx.states.OverrideBase;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author caddy
	 * 
	 */	
	public class FilterTitleWinMediator extends Mediator
	{				
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		[Inject]
		public var view:FilterTitleWindow;
		[Bindable]
		public var t:Number = 109;
		override public function onRegister():void
		{
			super.onRegister();		
            BindingUtils.bindProperty(view.distanceHS, "value", furnitureModel, "currentDistance");
			BindingUtils.bindProperty(view.alphaHS, "value", furnitureModel, "currentAlpha");
			BindingUtils.bindProperty(view.angleHS, "value", furnitureModel, "currentAngle");
			BindingUtils.bindProperty(view.xHS, "value", furnitureModel, "currentBlurX");
			BindingUtils.bindProperty(view.yHS, "value", furnitureModel, "currentBlurY");
			BindingUtils.bindProperty(view.strengthHS, "value", furnitureModel, "currentStrength");
			BindingUtils.bindProperty(view.qualityHS, "value", furnitureModel, "currentQuality");
			BindingUtils.bindProperty(view.color, "selectedColor", furnitureModel, "currentColor");
			
			addViewListener(FilterEvent.SET_FILTER, dispatch);
			eventMap.mapListener(view.setFilter, MouseEvent.CLICK, setFilterHandler, MouseEvent);
			eventMap.mapListener(view.closeButton, MouseEvent.CLICK, cancleHandler, MouseEvent);
			eventMap.mapListener(view.cancle, MouseEvent.CLICK, cancleHandler, MouseEvent);
		}
		
		/**
		 * @author caddy 
		 * @param event
		 * 取消阴影设置，还原为之前的状态
		 */		
		private function cancleHandler(event:MouseEvent):void
		{		
			view.dropShadow.distance = furnitureModel.currentDistance;
			view.dropShadow.alpha = furnitureModel.currentAlpha;
			view.dropShadow.angle = furnitureModel.currentAngle;
			view.dropShadow.blurX = furnitureModel.currentBlurX;
		    view.dropShadow.blurY = furnitureModel.currentBlurY;
			view.dropShadow.color = furnitureModel.currentColor;
			view.dropShadow.quality = furnitureModel.currentQuality;
			view.dropShadow.strength = furnitureModel.currentStrength;
			dispatch(new FilterEvent(FilterEvent.SET_FILTER, view.dropShadow));
			PopUpManager.removePopUp(view);
		}
		
		/**
		 * @author caddy
		 * @param event
		 * 设置某家具的阴影 
		 */		
		private function setFilterHandler(event:MouseEvent):void
		{
			furnitureModel.currentDistance = view.dropShadow.distance;
			furnitureModel.currentAlpha = view.dropShadow.alpha;
			furnitureModel.currentAngle = view.dropShadow.angle;
			furnitureModel.currentBlurX = view.dropShadow.blurX;
			furnitureModel.currentBlurY = view.dropShadow.blurY;
			furnitureModel.currentColor = view.dropShadow.color;
			furnitureModel.currentQuality = view.dropShadow.quality;
			furnitureModel.currentStrength = view.dropShadow.strength;
			
			dispatch(new FilterEvent(FilterEvent.SET_FILTER, view.dropShadow));
			PopUpManager.removePopUp(view);
		}		
	}
}